from datetime import datetime
import logging

from django.shortcuts import render_to_response, render

from accounts.models import User
from mall.models import Product
from system.models import Slider, News
from utils import constants

logger = logging.getLogger('index')



def index(request):
    """首页"""
    # print('my_user', request.my_user)
    # 记录调试信息
    logger.debug('调试信息')
    logger.info('普通消息')
    logger.error('异常消息')

    # 查询轮播图
    slider_list = Slider.objects.filter(types=constants.SLIDER_TYPE_INDEX)
    # 首页的新闻
    now_time = datetime.now()
    # 置顶的，有效的，在时间范围内的
    news_list = News.objects.filter(
        types=constants.NEWS_TYPE_NEW,
        is_top=True,
        is_valid=True,
        start_time__lte=now_time,
        end_time__gte=now_time)
    # 酒水推荐
    js_list = Product.objects.filter(
        status=constants.PRODUCT_STATUS_SELL,
        is_valid=True,
        tags__code='jstj'
    )
    # 精选推荐
    jx_list = Product.objects.filter(
        status=constants.PRODUCT_STATUS_SELL,
        is_valid=True,
        tags__code='jxtj'
    )
    # 猜你喜欢
    cn_list = Product.objects.filter(
        status=constants.PRODUCT_STATUS_SELL,
        is_valid=True,
        tags__code='cnxh'
    )
    # 从session中获取用户id
    # user_id = request.session[constants.LOGIN_SESSION_ID]
    # 查询当前登录的用户
    # user = User.objects.get(pk=user_id)
    return render(request, 'index.html', {
        'slider_list': slider_list,
        'news_list': news_list,
        'js_list': js_list,
        'jx_list': jx_list,
        'cn_list': cn_list
        # 'user': user
    })

